import BasePool from "../../db/BasePool";

export default class systemModel extends BasePool {

	/**
	 * --------------------------------------用户管理---------------------------------------
	 **/
	
	// 查询用户列表
	async getUserList(params){
		let limit = ``;
		let parameter:any = [];
		let search = ``;
		// 搜索
		if(params.name){
			let name = params.name.trim();
			search = ` and u.name like CONCAT(?,'%') `;
			parameter.push(name);
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql = `SELECT u.id, u.name, u.department, u.mobile, u.email, u.loginUser, u.createAt, u.status, u.roleid, u.loginTime, r.roleName FROM tbl_user u LEFT JOIN tbl_role r ON u.roleid = r.id AND r. STATUS = 1 WHERE u. STATUS != 0 ${search} ORDER BY u.createAt DESC ${limit}`;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 查询用户数量
	async getUserCount(params){
		let search = "";
		let name = "";
		if(params.name){
			name = params.name.trim();
			search = ` and name like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_user where status != 0"+search;
		let res:any = await this.query(sql,name);
		return res[0] ? res[0].c : 0;
	}
	// 查询账号是否存在
	async getUserByAccount(obj){
		let sql="select * from tbl_user where status!=0 and loginUser=?";
		if(obj.id){
			sql += " and id != ? "
		}
		let res = await this.query(sql,[obj.loginUser,obj.id]);
		return res;
	}
	// 编辑用户
	async editUser(obj){
		let sql="update tbl_user set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加用户
	async saveUser(obj){
		let sql="insert into tbl_user set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 禁用or启用
	async delUser(obj){
		let sql="update tbl_user set status=? where id=?";
		let res = await this.query(sql,[obj.status,obj.id]);
		return res;
	}
	
	

	/**
	 * ------------------------------------------角色管理---------------------------------------------
	 **/
	
	// 查询角色列表
	async getRoleList(params){
		let limit = ``;
		let parameter:any = [];
		let search = "";
		// 搜索
		if(params.roleName){
			let name = params.roleName.trim();
			search = " and roleName like CONCAT(?,'%') "
			parameter.push(name);
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql=`select * from tbl_role where status=1 ${search} order by createAt desc ${limit}`;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 查询角色数量
	async getRoleCount(params){
		let search = "";
		let name = "";
		if(params.roleName){
			name = params.roleName.trim();
			search = ` and name like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_role where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 角色列表不分页
	async getRoleListNopage(){
		let sql=`select * from tbl_role where status=1 order by createAt desc`;
		let res = await this.query(sql);
		return res;
	}
	// 编辑角色
	async editRole(obj){
		let sql="update tbl_role set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加角色
	async saveRole(obj){
		let sql="insert into tbl_role set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 保存菜单权限
	async saveRoleMenu(obj){
		let sql="insert into tbl_role_menu set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除该角色之前的菜单权限
	async delRoleMenu(roleid){
		let sql="update tbl_role_menu set status=0 where roleid=?";
		let res = await this.query(sql,roleid);
		return res;
	}
	// 删除角色
	async delRole(id){
		let sql="update tbl_role set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}
	// 查询某个角色权限
	async getMenuRole(params){
		let sql = `SELECT rm.*, m.pid FROM tbl_role_menu rm, tbl_menu m WHERE rm.menuid = m.id AND rm.status = 1 AND m.status = 1 AND rm.roleid = ?`
	  let res:any = await this.query(sql,params.id);
	  return res;
	}
	
	/**
	 * ----------------------------------------------菜单管理----------------------------------------------
	 **/
	
	// 菜单列表
	async getMenuList(){
	  let sql="select * from tbl_menu where status=1 order by sort";
	  let res:any = await this.query(sql);
	  return res;
	}
	// 添加菜单
	async saveMenu(obj){
		let sql="insert into tbl_menu set ?";
		let res = await this.query(sql,obj);
		return res;
	}
	// 添加菜单meta
	async saveMenuMeta(obj){
		let sql="insert into tbl_menu_meta set ?";
		let res = await this.query(sql,obj);
		return res;
	}
	// 编辑菜单
	async editMenu(obj){
		let sql="update tbl_menu set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 编辑菜单meta
	async editMenuMeta(obj){
		let sql="update tbl_menu_meta set ? where menuid=?";
		let res = await this.query(sql,[obj,obj.menuid]);
		return res;
	}
	// 删除菜单
	async delMenu(id){
		let sql="UPDATE tbl_menu m, tbl_menu_meta mm SET m. STATUS = 0, mm. STATUS = 0 WHERE m.id = mm.menuid AND m.id =?";
		let res = await this.query(sql,id);
		return res;
	}



	/**
	 * -----------------------------------------------部门管理-----------------------------------------------
	 **/

	// 部门列表
	async getDepartmentList(params){
		let search = "";
		let name = "";
		if(params.deptName){
			name = params.deptName.trim();
			search = " and deptName like CONCAT(?,'%') "
		}
		let sql="select * from tbl_department where status=1 "+search+" order by sort";
		let res = await this.query(sql,name);
		return res;
	}
	// 编辑部门
	async editDepartment(obj){
		let sql="update tbl_department set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加部门
	async saveDepartment(obj){
		let sql="insert into tbl_department set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除部门
	async delDepartment(id){
		let sql="update tbl_department set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}

}
